<?php 
	include("/includes/header.php");
	include("/includes/sidebar.php"); 

	$act = "edit";

	$id_en = $_GET[trim(base64_encode("id"), '=')]; 
	$id = base64_decode($id_en);

	if ($id != "") {
		$sql_user = "SELECT * FROM user WHERE id = $id";
		$query_user = mysql_query($sql_user);
		$result_user = mysql_fetch_array($query_user);
	}
?>

<form class="form-horizontal" data-id="<?php echo $id ?>">
	<div class="content row-fluid"> 
		<div class="span8">

			<input type="hidden" class="act" value="<?php echo $act; ?>">
			<div class="alert_container"></div>

    	<div>
    		<div>
    			<div class="alert alert-info">
    				<h4>
	    				<?php 
	    					if ($act == "edit") {
	    						echo "Edit User Form";
	    					} else {
	    						echo "New User Form";
	    					} 
	    				?>
    				</h4>
			    </div>
					<div class="control-group">
						<label class="control-label">Name</label>
						<div class="controls">
							<input placeholder="Input name" type="text" name="nama" class="nama span6 required" data-toggle="tooltip" title="Input name" required value="<?php echo $result_user[nama] ?>">
						</div>
					</div>
					<input type="hidden" class="idtipe_user" value="<?php echo $result_user[idtipe_user] ?>">
					<div class="control-group input-append">
						<label class="control-label">Username</label>
						<div class="controls">
							<input id="appendedInput" placeholder="Input Username" type="text" name="username" class="username span6 required" data-toggle="tooltip" title="Input username" required value="<?php echo $result_user[username] ?>">
							<span class="add-on user_check" title="Available check" data-toggle="tooltip">
								<i class="fa fa-eye"></i>
							</span>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Current Password</label>
						<div class="controls">
							<input placeholder="Input current password" type="password" name="current_password" class="current_password span6 required" data-toggle="tooltip" title="Input current password" required>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">New Password</label>
						<div class="controls">
							<input placeholder="Input new password" type="password" name="new_password" class="new_password span6 required" data-toggle="tooltip" title="Input new password" required>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">New Password Confirmation</label>
						<div class="controls">
							<input placeholder="Input new password confirmation" type="password" name="konfirmasi_password" class="konfirmasi_password span6 required" data-toggle="tooltip" title="Input new password confirmation" required>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Email</label>
						<div class="controls">
							<input placeholder="Input email" type="email" name="email" class="email span6" data-toggle="tooltip" title="Input email" value="<?php echo $result_user[email] ?>">
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Telepon</label>
						<div class="controls">
							<input placeholder="Input phone number" type="text" name="telepon" class="telepon span6" data-toggle="tooltip" title="Input phone number" value="<?php echo $result_user[telepon] ?>">
						</div>
					</div>
    		</div>
    	</div>
			<div>
				<div class="span12">
					<ul class="breadcrumb well">
						<button type="submit" class="btn btn-info btn-simpan">
							<i class="fa fa-check"></i>
							Save
						</button>
					</ul>
				</div>
			</div>
		</div>

		<div class="span4">
			<div class="alert alert-info">
				<h4>Profile Picture</h4>
	    </div>
	    <div class="foto_profil">
    		<img src="/img/default.jpg">
	    </div>
	    <div>
				<div class="span12">
					<ul class="breadcrumb well">
						<span class="btn btn-info btn-pilih">
							<i class="fa fa-white fa-upload"></i>
							Choose image
						</span>
					</ul>
				</div>
	    </div>
		</div>
	</div>
</form>

<div class="footer">
</div>

<?php include("/includes/footer.php") ?>       

<SCRIPT TYPE="text/javascript">

	$(document).ready(function() {
		$('.btn-simpan').click(function(event) {
			event.preventDefault();

			var confirm_valid = true;
			var name_valid = true;
			var pass_valid = true;

			if (validate_form()) {

				// ---------------- VALIDASI USERNAME ----------------
				var formUser = {
					id: $('form').data('id'),
					username: $('.username').val(),
					act: 4
				}

				$.ajax({
					url: '/action/act_user.php',
					type: 'POST',
					data: formUser,
					cache: false, 
			    success: function(data){
						if (data == 1) {
							alert_text("Username is already used", "danger");
							$('.alert_container .alert_val:last').slideDown();
							name_valid = false;
						}else {
							name_valid = true;
						}
						// alert("name = "+name_valid);

						// ---------------- VALIDASI CURRENT PASSWORD ----------------
						var formPass = {
							id: $('form').data('id'),
							currentPass: $('.current_password').val(),
							act: 5
						}

						$.ajax({
							url: '/action/act_user.php',
							type: 'POST',
							data: formPass,
							cache: false, 
					    success: function(data){
								if (data == 1) {
									alert_text("Current password do not match", "danger");
									$('.alert_container .alert_val:last').slideDown();
									pass_valid = false;
								}else{
									pass_valid = true;
								}
								// alert("pass = "+pass_valid);

								// ---------------- VALIDASI CONFIRMATION PASSWORD ----------------
								if ($('.new_password').val() != $('.konfirmasi_password').val()) {
									alert_text("Confirmation password do not match", "danger");
									$('.alert_container .alert_val:last').slideDown(function() {
										$('.konfirmasi_password').focus();
									});
									confirm_valid = false;
								}else{
									confirm_valid = true;	
								} 
								// alert("confirm = "+confirm_valid);

								if (name_valid && confirm_valid && pass_valid) {
									// alert("akses valid");
									var formUser = {
										id: $('form').data('id'),
										nama: $('.nama').val(),
										username: $('.username').val(),
										idtipe_user: $('.idtipe_user').val(),
										password: $('.new_password').val(),
										email: $('.email').val(),
										telepon: $('.telepon').val(),
										act: 2,
										act_form: "edit"
									}

									$.ajax({
										url: '/action/act_user.php',
										type: 'POST',
										data: formUser,
										cache: false, 
						        success: function(data){
						        	if (data == "sukses") {
							        	alert("Data has successfully edited");
							        	// window.location.reload();
						        	} else {
						        		alert(data);
						        	}
						        }
									})
								}
					    }
						})
			    }
				})
			};
		});

		$('.user_check').click(function(event) {
    	// $('.alert_container').hide();
			var name_valid = true;
			var formUser = {
				id: $('form').data('id'),
				username: $('.username').val(),
				act: 4
			}

			$.ajax({
				url: '/action/act_user.php',
				type: 'POST',
				data: formUser,
				cache: false, 
		    success: function(data){
					if (data == 0) {
						alert_text("Username available", "success");
						$('.alert_container .alert_val:last').slideDown();
					} else {
						alert_text("Username is already used", "danger");
						$('.alert_container .alert_val:last').slideDown();
						name_valid = false;
					}
		    }
			})
		});
	});
</SCRIPT>